﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using SQLToolbox.Controller.Tools;
using SQLToolbox.Types;

namespace SQLToolbox.Controller.UnitTests
{
	[TestFixture]
	public class SchemaComparatorUnitTests
	{
		[Test]
		public void TestCase()
		{
            var result = new SchemaComaprison();
            var left = new Schema();
            var right = new Schema();
            left.Tables.Add(new SchemaObject
            {
                Name = "Table1",
                Type = SchemaObjectType.Table,
                Schema = "dbo",
                Script = new Script("CREATE TABLE Table1")
            });
            left.Tables.Add(new SchemaObject
            {
                Name = "Table3",
                Type = SchemaObjectType.Table,
                Schema = "dbo",
                Script = new Script("CREATE TABLE Table3")
            });
            left.Tables[1].AttachedObjects.Add(new SchemaObject
            {
                Name = "Table3_Table1_FK",
                Type = SchemaObjectType.ForeignKey,
                Script = new Script("ALTER TABLE Table3 ADD CONSTRAIN Table3_Table1_FK")
            });

            right.Tables.Add(new SchemaObject
            {
                Name = "Table2",
                Type = SchemaObjectType.Table,
                Schema = "dbo",
                Script = new Script("CREATE TABLE Table2")
            });
            right.Tables.Add(new SchemaObject
            {
                Name = "Table3",
                Type = SchemaObjectType.Table,
                Schema = "dbo",
                Script = new Script("CREATE TABLE Table3")
            });
            right.Tables[1].AttachedObjects.Add(new SchemaObject
            {
                Name = "Table3_Table2_FK",
                Type = SchemaObjectType.ForeignKey,
                Script = new Script("ALTER TABLE Table3 ADD CONSTRAIN Table3_Table2_FK")
            });



            SchemaComparer.Compare(left, right, result);
            Assert.AreEqual(3, result.Count);
            Assert.AreEqual(ComparisonStatus.New,result[0].Status);
            Assert.AreEqual(ComparisonStatus.Missing, result[2].Status);
            Assert.AreEqual(ComparisonStatus.Different, result[1].Status);
		}
        [Test]
        public void TestCase1()
        {
            var leftobj = new SchemaObject
            {
                Name = "Table",
                Schema = "dbo",
                Type = SchemaObjectType.Table,
                Script = new Script("Create TABLE [table]")
            };

            var rightobj = new SchemaObject
            {
                Name = "Table",
                Schema = "dbo",
                Type = SchemaObjectType.Table,
                Script = new Script("Create TABLE [table]")
            };

            var result = SchemaComparer.CompareObjects(leftobj, rightobj);
            Assert.AreEqual(ComparisonStatus.Equal, result.Status);
        }

        [Test]
        public void TestNulls()
        {
            SchemaObject leftobj = null;
            SchemaObject rightobj = null;
            Assert.IsNull(SchemaComparer.CompareObjects(leftobj, rightobj));
        }
        
	}
}
